package com.hoolai.qps.mina.proto;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;

import com.hoolai.qps.mina.proto.Client.MessageProcess;
import com.hoolai.qps.proto.ReqCmdProto.SaveUserReqCmdProto;
import com.hoolai.qps.utils.StringGenerate;

public class LoopClient {

    private static final Logger logger = Logger.getLogger(LoopClient.class);

    public static void main(String[] args) throws InterruptedException {
        ExecutorService server = Executors.newCachedThreadPool();
        CopyOnWriteArrayList<Callable<String>> tasks = new CopyOnWriteArrayList<Callable<String>>();
        for (int i = 0; i < 50; i++) {
            Callable<String> callable = new Callable<String>() {
                @Override
                public String call() throws Exception {
                    new Client(new MessageProcess() {
                        @Override
                        public void sendMessage(IoSession session) {
                            while (true) {
                                SaveUserReqCmdProto.Builder builder = SaveUserReqCmdProto.newBuilder();
                                builder.setName(StringGenerate.message);
                                session.write(builder.build());
                                try {
                                    TimeUnit.SECONDS.sleep(1);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    });
                    return "";
                }
            };

            tasks.add(callable);
        }
        List<Future<String>> result = server.invokeAll(tasks);
        logger.info(result);
        logger.info("ok");
    }
}
